<template>
  <popupBox ref="serviceTimePopup" type="bottom" :isSlot="true">
    <view class="service-time-box">
      <view class="header">
        <view class="title display-flex alig center">
          <text>请选择服务时间</text>
        </view>
        <view class="close">
          <image
            :src="$img_path('/course/close_popup.png')"
            @click="close"
            mode="widthFix"
          ></image>
        </view>
      </view>
      <view class="content">
        <!-- 左右滚动日期 -->
        <view class="more-swiper-box mt-20">
          <scroll-view class="more-swiper_H" scroll-x="true">
            <view
              class="more-swiper-item_H"
              :class="{ 'top-time-active': item.date == topValue.date }"
              v-for="(item, index) in topDate"
              :key="index"
              @click="selectTopDate(item)"
            >
              <view class="center-col" style="height: 100%">
                <view class="">
                  <text>{{ item.weekday }}</text>
                </view>
                <view class="">
                  <text>{{ formattedDate(item.date) }}</text>
                </view>
              </view>
            </view>
          </scroll-view>
        </view>
        <!-- 事件 -->
        <view class="time-box mt-20">
          <view
            class="time-item center-col"
            :class="{
              'time-disabled': item.status == '约满' && item.isToday,
              'time-item-active': timeValue.time == item.time,
            }"
            v-for="item in times"
            :key="item"
            @click="selectTime(item)"
          >
            {{ item.time }}
            <view class="">
              <text></text>
              <text v-if="item.isNight">夜间费{{ nightMoney }}</text>
              <text v-else-if="item.isToday">{{ item.status }}</text>
              <text v-else></text>
            </view>
          </view>
        </view>
      </view>
      <view class="fixed-btn-box">
        <view class="fixed-btn common-btn-bg-color" @click="confirm">
          <text>确定</text>
        </view>
      </view>
    </view>
  </popupBox>
</template>

<script>
import popupBox from "@/components/popup/popup";
import {
  generateDateArray,
  generateHalfHourlyTimeArrayWithStatus,
} from "./utils/date.js";
export default {
  components: {
    popupBox,
  },
  props: {
    nightMoney: {
      type: Number | String,
      default: 0,
    },
  },
  data() {
    return {
      topDate: [],
      times: [],
      topValue: {
        date: "",
        weekday: "",
      },
      timeValue: {
        time: "",
        money: 0,
      },
    };
  },
  mounted() {
    this.topDate = generateDateArray(30);
    setTimeout(() => {
      this.topValue = this.topDate[0];
      console.log(this.topDate[0]);
      this.times = generateHalfHourlyTimeArrayWithStatus(this.topValue.date);
    });
  },
  methods: {
    formattedDate(item) {
      const parts = item.split("-");
      return `${parts[0]}月${parts[1]}日`;
    },
    open() {
      this.$refs.serviceTimePopup.open();
    },
    close() {
      this.$refs.serviceTimePopup.close();
    },
    confirm() {
      this.$emit("confirm", {
        date: this.topValue,
        time: this.timeValue,
      });
      this.close();
    },
    selectTopDate(item) {
      this.topValue = item;
      this.times = [];
      this.$nextTick(() => {
        this.times = generateHalfHourlyTimeArrayWithStatus(this.topValue.date);
        this.timeValue = {
          time: "",
          money: 0,
        };
      });
    },
    selectTime(time) {
      if (time.status === "约满" && time.isToday) return;
      this.timeValue = time;
    },
  },
};
</script>

<style scoped lang="scss">
.service-time-box {
  // height: 95vh;
  overflow: hidden;
  background-color: #fff;
  padding: 20rpx;
}

.header {
  .title {
    font-weight: bold;
    font-size: 36rpx;
    color: #333333;
  }

  .close {
    width: 32rpx;
    height: 32rpx;
    position: absolute;
    right: 20rpx;
    top: 30rpx;
  }
}

.content {
  padding-bottom: 200rpx;
}

.fixed-btn-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 22rpx;
  padding-bottom: 68rpx;
}

.fixed-btn {
  height: 96rpx;
  border-radius: 80rpx;
  font-weight: bold;
  font-size: 36rpx;
  color: #ffffff;
  text-align: center;
  line-height: 96rpx;
}

.more-swiper_H {
  white-space: nowrap;
  width: 100%;
}

.more-swiper-item_H {
  display: inline-block;
  width: 170rpx;
  height: 160rpx;
  background: #f8f8f8;
  border-radius: 0rpx 0rpx 0rpx 0rpx;
  border: 2rpx solid #f1f1f1;
}

.center-col {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.top-time-active {
  background: linear-gradient(180deg, #ffa233 0%, #fb4c4c 100%);
  border: 2rpx solid #f1f1f1;
  color: #fff;
}

.time-box {
  height: 652rpx;
  display: flex;
  flex-wrap: wrap;
  overflow: auto;
}

.time-item {
  width: calc(100% / 4);
  height: 114rpx;
  background: #ffffff;
  border: 2rpx solid #f1f1f1;
  font-weight: 400;
  font-size: 30rpx;
  color: #999999;
}

.time-item-dis {
  background: #f8f8f8;
  border: 2rpx solid #f1f1f1;
}

.time-item-active {
  background: linear-gradient(180deg, #ffa233 0%, #fb4c4c 100%);
  color: #fff;
}

.time-disabled {
  background: #f8f8f8;
}
</style>